<?php

class class_Login {

var $user_id = null;
var $user = null;
var $pass = null;
var $level = null;
var $ok = false;

function getData($u,$p)
{
 $this->user = $u;
 $this->pass = $p;

}

  function check_session()
{
if(!empty($_SESSION['auth_username']) && !empty($_SESSION['auth_password']))
return $this->check($_SESSION['auth_username'], $_SESSION['auth_password']);
else
return false;
}

 function login()
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query_params($conn, 'SELECT id,userlevel FROM users WHERE name = $1 AND password = $2' , array($this->user,$this->pass) );
if (1 == pg_num_rows($result))
{
 $_SESSION['auth_username'] = $this->user;
 $_SESSION['auth_password'] = $this->pass;
 $row = pg_fetch_object($result);
 $_SESSION['auth_id'] = $row->id;
 $_SESSION['auth_level'] = $row->userlevel;
 return true;
}
else {
return false;
}
}	

 function check($username, $password)
{
include_once 'admin/config.php';
$conn = db();
$result = pg_query_params($conn, 'SELECT id FROM users WHERE name = $1 AND password = $2' , array($username,$password) );
if (1 == pg_num_rows($result))
{
 return true;
} else {
return false;
}

}
 
function logout()
{
session_start();
$this->user_id = 0;
$this->username = "Guest";
$this->ok = false;
 
$_SESSION['auth_username'] = "";
$_SESSION['auth_password'] = "";
session_unset();
session_destroy(); 

return true;

}

 
}
